package com.qmerak.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.qmerak.common.SimpleJdbcTestUtils;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-4-6
 * @project TUManager
 */
@SuppressWarnings("deprecation")
public abstract class BaseDao {
	private static Logger logger = Logger.getLogger(BaseDao.class);

	@Autowired
	private DataSource dataSource;

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Autowired
	protected SimpleJdbcTemplate simpleJdbcTemplate;

	@Autowired
	protected DataSourceTransactionManager transactionManager;

	@Autowired
	protected SessionFactory sessionFactory;

	// protected HibernateTemplate hibernateTemplate;

	public void initDB() {
		SimpleJdbcTestUtils.executeSqlScript(simpleJdbcTemplate,
				new FileSystemResource(""), false);
	}

	public void clearDB() {
		SimpleJdbcTestUtils.executeSqlScript(simpleJdbcTemplate,
				new FileSystemResource(""), false);
	}

	protected boolean insert() {
		return false;
	}

	protected List<Hashtable<String, Object>> query(String sql) {
		// System.out.println("===JdbcTemplate:" + this.jdbcTemplate);
		// System.out.println("===SimpleJdbcTemplate:" +
		// this.simpleJdbcTemplate);
		// System.out.println("===DataSourceTransactionManager:" +
		// this.transactionManager);
		// System.out.println("===sessionFactory:" + this.sessionFactory);
		List<Hashtable<String, Object>> result = new LinkedList<Hashtable<String, Object>>();
		Connection connection = null;
		Statement statement = null;
		try {
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			ResultSet set = statement.executeQuery(sql);
			result = this.resultToList(set);
			System.out.println("查询结束!!!" + result);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 将数据集合转换为列表
	 * 
	 * @param set
	 * @return
	 */
	protected List<Hashtable<String, Object>> resultToList(ResultSet set) {
		List<Hashtable<String, Object>> list = new LinkedList<Hashtable<String, Object>>();
		try {
			ResultSetMetaData data = set.getMetaData();
			while (set.next()) {
				Hashtable<String, Object> table = new Hashtable<String, Object>();
				for (int i = 1; i <= data.getColumnCount(); i++) {
					String name = data.getColumnName(i);
					Object object = set.getObject(name);
					if (object != null) {
						table.put(name, object);
					}
				}
				list.add(table);
			}
		} catch (SQLException e) {
			logger.error("将数据集合转换为列表错误\n" + e.getMessage());
			throw new RuntimeException(e);
		}
		return list;
	}

}
